
package com.bluelinelabs.conductor.conductor.internal;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ListContainer;


/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class ViewAttachHandler implements Component.BindStateChangedListener {

    private boolean rootAttached = false;
    boolean childrenAttached = false;
    private boolean activityStopped = false;
    private ReportedState reportedState = ReportedState.VIEW_DETACHED;
    private ViewAttachListener attachListener;
    Component.BindStateChangedListener childOnAttachStateChangeListener;


    @Override
    public void onComponentBoundToWindow(Component component) {
        if (rootAttached) {
            return;
        }

        rootAttached = true;
        listenForDeepestChildAttach(component, new ChildAttachListener() {
            @Override
            public void onAttached() {
                childrenAttached = true;
                reportAttached();
            }
        });
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        rootAttached = false;
        if (childrenAttached) {
            childrenAttached = false;
            reportDetached(false);
        }

    }


    private interface ChildAttachListener {
        void onAttached();
    }

    private enum ReportedState {
        VIEW_DETACHED,
        ACTIVITY_STOPPED,
        ATTACHED
    }


    public interface ViewAttachListener {
        void onAttached();

        void onDetached(boolean fromActivityStop);

        void onViewDetachAfterStop();
    }

    public ViewAttachHandler(ViewAttachListener attachListener) {
        this.attachListener = attachListener;
    }


    public void listenForAttach(final Component view) {
        view.setBindStateChangedListener(this);
    }


    public void unregisterAttachListener(Component view) {
        if (view != null) {
            view.removeBindStateChangedListener(this);
        }

        if (childOnAttachStateChangeListener != null && view instanceof ComponentContainer) {
            findDeepestChild((ComponentContainer) view).removeBindStateChangedListener(childOnAttachStateChangeListener);
            childOnAttachStateChangeListener = null;
        }
    }

    public void onActivityStarted() {
        activityStopped = false;
        reportAttached();
    }

    public void onActivityStopped() {
        activityStopped = true;
        reportDetached(true);
    }

    void reportAttached() {
        if (rootAttached && childrenAttached && !activityStopped && reportedState != ReportedState.ATTACHED) {
            reportedState = ReportedState.ATTACHED;
            attachListener.onAttached();
        }
    }

    private void reportDetached(boolean detachedForActivity) {
        boolean wasDetachedForActivity = reportedState == ReportedState.ACTIVITY_STOPPED;

        if (detachedForActivity) {
            reportedState = ReportedState.ACTIVITY_STOPPED;
        } else {
            reportedState = ReportedState.VIEW_DETACHED;
        }

        if (wasDetachedForActivity && !detachedForActivity) {
            attachListener.onViewDetachAfterStop();
        } else {
            attachListener.onDetached(detachedForActivity);
        }
    }


    private void listenForDeepestChildAttach(final Component view, final ChildAttachListener attachListener) {
//        childrenAttached = true;
//        LogUtils.show(this,"什么鬼 listenForDeepestChildAttach 123 childrenAttached = " + childrenAttached);
//        attachListener.onAttached();
//        LogUtils.show(this,"什么鬼 listenForDeepestChildAttach 123 childrenAttached = " + childrenAttached);
        if (!(view instanceof ComponentContainer)) {
            attachListener.onAttached();
            return;
        }

        ComponentContainer viewGroup = (ComponentContainer) view;
        if (viewGroup.getChildCount() == 0) {
            attachListener.onAttached();
            return;
        }


        childOnAttachStateChangeListener = new Component.BindStateChangedListener() {
            boolean attached = false;

            @Override
            public void onComponentBoundToWindow(Component v) {
                if (!attached && childOnAttachStateChangeListener != null) {
                    attached = true;
                    attachListener.onAttached();
//                    v.removeBindStateChangedListener(this);
                    childOnAttachStateChangeListener = null;
                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }


        };
        Component deepestChild = findDeepestChild(viewGroup);
        if (deepestChild != null) {
            deepestChild.setBindStateChangedListener(childOnAttachStateChangeListener);
        }
    }

    private Component findDeepestChild(ComponentContainer viewGroup) {
        if (viewGroup.getChildCount() == 0) {
            return viewGroup;
        }
        Component lastChild = viewGroup.getComponentAt(viewGroup.getChildCount() - 1);
        //TODO 严重的bug必须加!(lastChild instanceof ListContainer)解决
        if (lastChild instanceof ComponentContainer && !(lastChild instanceof ListContainer)) {
            return findDeepestChild((ComponentContainer) lastChild);
        } else {
            return lastChild;
        }
    }
}
